/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#include "ftcontrols.h"
#include "ftlabel.h"

FTLabel::FTLabel(const QString& s, QWidget *parent) :
    QWidget(parent)
{
    setText(s);
    setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed );
    _alignment = Qt::AlignLeft;
    isTitle = false;
}

void FTLabel::setTitle(bool t)
{
    isTitle = t;
    if ( isTitle )
        setFont( FTControls::font( FTControls::FontTitle ) );
    else
        setFont( FTControls::font( FTControls::FontNormal ) );
    update();
}

QString FTLabel::text() const
{
    return _text;
}

void FTLabel::setText(const QString & s )
{
    if ( s != _text )
    {
        _text = s;
        update();
    }
}

QSize FTLabel::sizeHint() const
{
    int padding = FTControls::defaultPadding();
    QFontMetrics fm(font());
    QSize size = fm.size( Qt::TextSingleLine, _text )+
                 QSize( padding*2,
                        padding);
    return size;
}

QSize FTLabel::minimumSizeHint() const
{
    QSize size = sizeHint();
    size.setWidth( 5 );
    return size;
}

void FTLabel::paintEvent ( QPaintEvent * )
{
    int padding = FTControls::defaultPadding();
    QPalette palette = QApplication::palette();
    palette.setCurrentColorGroup( FT_COLOR_GROUP );
    QPainter painter(this);
    QRect border = rect().adjusted(1,1,-2,-2);

    border = painter.boundingRect( border.adjusted( padding,
                                                    padding>>1,
                                                    -padding,
                                                    -(padding>>1)),
                                   _alignment | Qt::AlignVCenter | Qt::TextSingleLine,
                                   _text);

    painter.setPen( palette.color( QPalette::WindowText ) );
    painter.drawText( border, _alignment | Qt::AlignVCenter | Qt::TextSingleLine, _text );
}

int FTLabel::alignment() const
{
    return _alignment;
}

void FTLabel::setAlignment( int a )
{
    _alignment = a;
    update();
}
